package com.sven.player;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.media.MediaPlayer;
import android.os.CountDownTimer;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.sven.player.utils.TimeUtil;

import java.io.IOException;

/**
 * Created by Sven on 2016/10/13 0013.
 */

public class SmallVoicePlayer extends FrameLayout {

    private ImageView mIv_Play;
    private TextView mTv_Time;
    private VoiceClickListener voiceClickListener;
    private CountDownTimer timer;

    public void setmGender(boolean mGender) {
        this.mGender = mGender;
        if (mGender) {
            mIv_Play.setBackgroundResource(R.drawable.ic_play_voice_boy);
        } else {
            mIv_Play.setBackgroundResource(R.drawable.ic_play_voice_girl);
        }
        invalidate();
    }

    private long mDuration;
    private boolean mGender = true;

    public SmallVoicePlayer(Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.small_view_player, this);
    }

    public SmallVoicePlayer(Context context, AttributeSet attrs) {
        super(context, attrs);
        getAttrs(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.small_view_player, this);
    }

    public SmallVoicePlayer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        getAttrs(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.small_view_player, this);
    }

    private void getAttrs(Context context, AttributeSet attrs) {
        TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.SmallVoicePlayer);
        mGender = t.getBoolean(R.styleable.SmallVoicePlayer_gender, true);

        t.recycle();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mIv_Play = (ImageView) findViewById(R.id.iv_play);
        mTv_Time = (TextView) findViewById(R.id.tv_time);
        if (mGender) {
            mIv_Play.setBackgroundResource(R.drawable.ic_play_voice_boy);
        } else {
            mIv_Play.setBackgroundResource(R.drawable.ic_play_voice_girl);
        }
    }


    public void play(String recordPath, long ms) {
        this.mDuration = ms;
        voiceClickListener = new VoiceClickListener(recordPath);
        mIv_Play.setOnClickListener(voiceClickListener);
        mTv_Time.setText(TimeUtil.toTimeStr((int) ((ms / 1000) + 0.5)));
    }

    private void startCountDownTime(long time) {
        timer = new CountDownTimer(time, 1000) {
            @SuppressLint("SetTextI18n")
            @Override
            public void onTick(long millisUntilFinished) {
                mTv_Time.setText(TimeUtil.toTimeStr((int) ((millisUntilFinished / 1000) + 0.5)));
            }

            @Override
            public void onFinish() {
                mTv_Time.setText(TimeUtil.toTimeStr((int) ((mDuration / 1000) + 0.5)));
            }
        };
        timer.start();// 开始计时
    }

    public class VoiceClickListener implements OnClickListener {

        private MediaPlayer mp;

        private String url;

        public MediaPlayer getMp() {
            return mp;
        }

        public VoiceClickListener(String url) {
            this.url = url;
        }

        @Override
        public void onClick(final View v) {

            if (mGender) {
                if (mIv_Play.getBackground().getConstantState() == (ContextCompat.getDrawable(getContext(), R.drawable.ic_play_voice_boy)).getConstantState()) {
                    Log.e("VoiceClickListener", "mDuration:" + mDuration);
                /*默认未播放状态*/
                    mIv_Play.setBackgroundResource(R.drawable.ic_stop_voice_boy);
                    try {
                        mp = new MediaPlayer();
                        mp.setDataSource(url);
                        mp.prepare();
                        mp.start();
                        startCountDownTime(mDuration);
                    } catch (IllegalArgumentException | IllegalStateException | IOException e) {
                        e.printStackTrace();
                    }

                    mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp1) {
                            mIv_Play.setBackgroundResource(R.drawable.ic_play_voice_boy);
                            if (timer != null) {
                                timer.onFinish();
                            }
                            mp1.release();
                        }
                    });
                } else {
                /*播放状态*/
                    stop();
                }
            } else {
                if (mIv_Play.getBackground().getConstantState() == (ContextCompat.getDrawable(getContext(), R.drawable.ic_play_voice_girl)).getConstantState()) {
                    Log.e("VoiceClickListener", "mDuration:" + mDuration);
                /*默认未播放状态*/
                    mIv_Play.setBackgroundResource(R.drawable.ic_stop_voice_girl);
                    try {
                        mp = new MediaPlayer();
                        mp.setDataSource(url);
                        mp.prepare();
                        mp.start();
                        startCountDownTime(mDuration);
                    } catch (IllegalArgumentException | IllegalStateException | IOException e) {
                        e.printStackTrace();
                    }

                    mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp1) {
                            mIv_Play.setBackgroundResource(R.drawable.ic_play_voice_girl);
                            if (timer != null) {
                                timer.onFinish();
                            }
                            mp1.release();
                        }
                    });
                } else {
                /*播放状态*/
                    stop();
                }
            }


        }

        public void stop() {
            if (mGender) {
                mIv_Play.setBackgroundResource(R.drawable.ic_play_voice_boy);
                if (voiceClickListener.getMp() != null) {
                    voiceClickListener.getMp().release();
                }
            } else {
                mIv_Play.setBackgroundResource(R.drawable.ic_play_voice_girl);
                if (voiceClickListener.getMp() != null) {
                    voiceClickListener.getMp().release();
                }
            }
            if (timer != null) {
                timer.onFinish();
                timer.cancel();
            }

        }
    }
}
